﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Xml;

namespace medicine.General
{
    public static class SerializingUtility
    {
        public static void SerializeObjectToFile<T>(T obj, string path)
        {
                TextWriter writer = new StreamWriter(path);
                XmlSerializer  xs= new XmlSerializer(typeof(T));
                xs.Serialize(writer, obj);
                writer.Close();
        }

        public static T DeserializeObjectFromFile<T>(string path)
        {
            FileStream fs = File.OpenRead(path);
            XmlSerializer xs = new XmlSerializer(typeof(T));
            T result=(T)xs.Deserialize(fs);
            fs.Close();
            return result;

        }


        public static string SerializeObjectToXMLString<T>(T obj)
        {
            try
            {
                string xmlString = null;
                MemoryStream memoryStream = new MemoryStream();
                XmlSerializer xs = new XmlSerializer(typeof(T));
                XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
                xs.Serialize(xmlTextWriter, obj);
                memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
                xmlString = UTF8ByteArrayToString(memoryStream.ToArray());

                xmlString = xmlString.Substring(1);
                if (!xmlString.StartsWith("<"))
                    xmlString = "<" + xmlString;
                return xmlString;
            }
            catch
            {
                return string.Empty;
            }
        }


        public static T DeserializeObjectFromXMLString<T>(string xml)
        {
            XmlSerializer xs = new XmlSerializer(typeof(T));
            MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(xml));
            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
            return (T)xs.Deserialize(memoryStream);
        }

        public static string SerializeObjectToXMLString2<T>(T obj)
        {
            StringWriter SW = new StringWriter();
            XmlSerializer XS = new XmlSerializer(typeof(T));
            XS.Serialize(SW, obj);
            return SW.ToString();
        }

        public static T DeserializeObjectFromXMLString2<T>(string XML)
        {
            StringReader SR = new StringReader(XML);

            XmlSerializer XS = new XmlSerializer(typeof(T));
            return (T)XS.Deserialize(SR);
        }


        private static string UTF8ByteArrayToString(byte[] characters)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            string constructedString = encoding.GetString(characters);
            return (constructedString);
        }

        private static Byte[] StringToUTF8ByteArray(string pXmlString)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            byte[] byteArray = encoding.GetBytes(pXmlString);
            return byteArray;
        }

        public static void SaveToTextFile(string FileName, string FileData,bool Append)
        {
            TextWriter tw = new StreamWriter(FileName, Append);
            tw.Write(FileData);
            tw.Close();
            
        }

        public static void ShellOpenFile(string FileName)
        {
            System.Diagnostics.Process.Start(FileName);
        }
    }
}
